(function(e){if(e.BX.webrtc)return;var t=e.BX;t.webrtc=function(){this.debug=false;this.audioMuted=false;this.videoMuted=false;this.enabled=false;this.detectedBrowser="none";this.pcConfig={};this.oneway=false;this.lastUserMediaParams={};this.pcConstraints={};this.sdpConstraints={mandatory:{OfferToReceiveAudio:true,OfferToReceiveVideo:true}};this.defaultMicrophone=null;this.defaultCamera=null;this.defaultSpeaker=null;this.enableMicAutoParameters=true;this.useFallbackConstraints=false;this.configVideo={aspectRatio:1.7777777778};this.configVideoGroup=this.configVideo;this.configVideoMobile=this.configVideo;this.configVideoAfterError={};this.callStreamSelf=null;this.callStreamMain=null;this.callStreamUsers={};this.pc={};this.pcStart={};this.connected={};this.iceCandidates={};this.initiator=false;this.callUserId=0;this.callChatId=0;this.callToGroup=false;this.callGroupUsers=[];this.callInit=false;this.callInitUserId=0;this.callActive=false;this.callVideo=false;this.callRequestUserMedia={};this.needPeerConnection=true;this.createAnswerTimeout={};this.initPeerConnectionTimeout={};this.iceCandidateTimeout={};this.pcConnectTimeout={};this.init();this.setTurnServer()};t.inheritWebrtc=function(e){e.prototype=Object.create(t.webrtc.prototype);e.prototype.constructor=e;e.prototype.parent=t.webrtc.prototype};t.webrtc.prototype.init=function(){if(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia)this.enabled=true;else return;if(navigator.userAgent.substr(navigator.userAgent.indexOf("Firefox/")+8,2)>=27)this.detectedBrowser="firefox";else if(navigator.appVersion.substr(navigator.appVersion.indexOf("Chrome/")+7,2)>=29)this.detectedBrowser="chrome"};t.webrtc.prototype.attachMediaStream=function(e,i){e.srcObject=i;if(t.type.isNotEmptyString(this.defaultSpeaker)&&e.setSinkId){this.log("Trying to set output device: "+this.defaultSpeaker);e.setSinkId(this.defaultSpeaker)}};t.webrtc.prototype.ready=function(){return this.enabled};t.webrtc.prototype.setTurnServer=function(e){if(!this.ready())return false;e=e||{};this.turnServer=e.turnServer||"turn.calls.bitrix24.com";this.turnServerFirefox=e.turnServerFirefox||"54.217.240.163";this.turnServerLogin=e.turnServerLogin||"bitrix";this.turnServerPassword=e.turnServerPassword||"bitrix";if(this.detectedBrowser=="firefox"){this.pcConfig={iceServers:[{url:"stun:"+this.turnServerFirefox},{url:"turn:"+this.turnServerFirefox,credential:this.turnServerPassword,username:this.turnServerLogin}]};this.pcConstraints={optional:[{DtlsSrtpKeyAgreement:true}]}}else if(this.detectedBrowser=="chrome"){this.pcConfig={iceServers:[{url:"stun:"+this.turnServer},{url:"turn:"+this.turnServerLogin+"@"+this.turnServer,username:this.turnServerLogin,credential:this.turnServerPassword}]};this.pcConstraints={optional:[{DtlsSrtpKeyAgreement:true}]}}return true};t.webrtc.prototype.toggleAudio=function(e){if(!this.ready())return false;e=typeof e!="undefined"?e:true;if(e)this.audioMuted=this.audioMuted?false:true;if(this.callStreamSelf&&this.callStreamSelf.getAudioTracks()&&this.callStreamSelf.getAudioTracks().length>0){for(var t=0;t<this.callStreamSelf.getAudioTracks().length;t++){this.callStreamSelf.getAudioTracks()[t].enabled=!this.audioMuted}}return true};t.webrtc.prototype.toggleVideo=function(e){if(!this.ready())return false;e=typeof e!="undefined"?e:true;if(e)this.videoMuted=this.videoMuted?false:true;if(this.callStreamSelf&&this.callStreamSelf.getVideoTracks()&&this.callStreamSelf.getVideoTracks().length>0){for(var t=0;t<this.callStreamSelf.getVideoTracks().length;t++){this.callStreamSelf.getVideoTracks()[t].enabled=!this.videoMuted}}return true};t.webrtc.prototype.signalingReady=function(){return this.ready()&&t.PULL&&t.PULL.getPullServerStatus()};t.webrtc.prototype.log=function(){var e="";if(t.desktop&&t.desktop.ready()){for(var i=0;i<arguments.length;i++){try{e=e+" | "+(typeof arguments[i]=="object"?JSON.stringify(arguments[i]):arguments[i])}catch(t){e=e+" | (circular structure)"}}t.desktop.log(t.message("USER_ID")+".video.log",e.substr(3))}if(this.debug){if(console)console.log("WebRTC Log",arguments)}};t.webrtc.prototype.startGetUserMedia=function(e,i){if(this.oneway&&!this.initiator){this.onUserMediaSuccess();return true}this.lastUserMediaParams={video:e,audio:i};if(this.callRequestUserMedia[this.callVideo?"video":"audio"])return false;if(typeof e!="undefined"&&e!==true){}else{if(this.useFallbackConstraints)e=this.configVideoAfterError;else if(this.callToGroup)e=this.configVideoGroup;else e=this.configVideo}if(e&&this.defaultCamera){e.deviceId={ideal:this.defaultCamera}}i=typeof i!="undefined"&&i!==true?i:{};if(i&&this.defaultMicrophone){i.deviceId={ideal:this.defaultMicrophone}}if(false&&this.enableMicAutoParameters===false){i.optional=[{echoCancellation:false},{googEchoCancellation:false},{googEchoCancellation2:false},{googDAEchoCancellation:false},{googAutoGainControl:false},{googAutoGainControl2:false},{mozAutoGainControl:false},{googNoiseSuppression:false},{googNoiseSuppression2:false},{googHighpassFilter:false},{googTypingNoiseDetection:false},{googAudioMirroring:false}]}var n={audio:i,video:e};this.log('Requested access to local media with constraints:  "'+JSON.stringify(n)+'"');try{this.callRequestUserMedia[this.callVideo?"video":"audio"]=true;navigator.mediaDevices.getUserMedia(n).then(t.delegate(this.onUserMediaSuccess,this),t.delegate(this.onUserMediaError,this))}catch(e){this.debug=true;this.log("Method getUserMedia failed with exception: "+e.message)}return true};t.webrtc.prototype.onUserMediaSuccess=function(e){this.log("Media stream received");if(e&&e.getTracks){e.getTracks().forEach(function(e){this.log(t.webrtc.mediaStreamTrackToString(e))}.bind(this))}if(!this.oneway||this.initiator){this.callRequestUserMedia[this.callVideo?"video":"audio"]=false;if(this.callStreamSelf)return false;if(!this.callActive&&e){t.webrtc.stopMediaStream(e);return false}this.log("User has granted access to local media.");this.callStreamSelf=e;this.toggleAudio(false)}if(!this.needPeerConnection)return true;if(this.initiator){if(this.callToGroup){for(var i=0;i<this.callGroupUsers.length;i++){var n=this.callGroupUsers[i];if(n!=t.message("USER_ID")){clearTimeout(this.initPeerConnectionTimeout[n]);this.initPeerConnection(n)}}}else{clearTimeout(this.initPeerConnectionTimeout[this.callUserId]);this.initPeerConnection(this.callUserId)}}else{if(this.callToGroup){for(var i=0;i<this.callGroupUsers.length;i++){var n=this.callGroupUsers[i];if(n!=t.message("USER_ID")&&n!=this.callInitUserId&&!this.callStreamUsers[n]){clearTimeout(this.initPeerConnectionTimeout[n]);this.initPeerConnection(n,true)}}}}return true};t.webrtc.prototype.onUserMediaError=function(e){this.debug=true;this.callRequestUserMedia[this.callVideo?"video":"audio"]=false;if(!this.callActive)return false;this.log("Failed to get access to local media. Error code was "+JSON.stringify(e));return true};t.webrtc.prototype.initPeerConnection=function(e,i){if(!this.callActive)return false;i=i===true;if(this.debug)console.log(e,"wait initPeerConnection");if(!this.pcStart[e]&&(!this.oneway&&this.callStreamSelf||this.oneway&&this.initiator&&this.callStreamSelf||this.oneway&&!this.initiator)){if(this.connected[e]){this.log("Creating PeerConnection.",e);this.createPeerConnection(e);if(!this.pc[e]){clearTimeout(this.initPeerConnectionTimeout[e]);this.initPeerConnectionTimeout[e]=setTimeout(t.delegate(function(){this.initPeerConnection(e,i)},this),2e3);return false}this.log("Adding local stream.",e,this.pc[e]);this.pcStart[e]=true;if(this.initiator||i)this.sendOfferToPeer(e)}else{clearTimeout(this.initPeerConnectionTimeout[e]);this.initPeerConnectionTimeout[e]=setTimeout(t.delegate(function(){this.initPeerConnection(e,i)},this),2e3)}}else if(!this.connected[e]){clearTimeout(this.initPeerConnectionTimeout[e]);this.initPeerConnectionTimeout[e]=setTimeout(t.delegate(function(){this.initPeerConnection(e,i)},this),2e3)}return true};t.webrtc.prototype.setLocalAndSend=function(e,i){if(this.pc[e]==null||!this.callActive)return false;this.log("set local description and send",e,JSON.stringify(i));this.pc[e].setLocalDescription(i,t.delegate(function(e){this.log("setLocalDescription",e)},this),t.delegate(function(e){this.log("setLocalDescription",e)},this));return true};t.webrtc.prototype.onRemoteStreamAdded=function(e,t,i){};t.webrtc.prototype.onRemoteStreamRemoved=function(e,t){};t.webrtc.prototype.onIceCandidate=function(e,t){};t.webrtc.prototype.onIceConnectionStateChange=function(e,t){};t.webrtc.prototype.onSignalingStateChange=function(e,t){};t.webrtc.prototype.peerConnectionError=function(e,t){};t.webrtc.prototype.peerConnectionReconnect=function(e){if(!this.pc[e])return false;if((this.pc[e].iceConnectionState=="connected"||this.pc[e].iceConnectionState=="completed")&&this.pc[e].signalingState=="stable")return false;this.log("peerConnectionReconnect",this.pc[e].iceConnectionState,this.pc[e].signalingState);this.pc[e].close();delete this.pc[e];delete this.pcStart[e];if(this.callStreamMain==this.callStreamUsers[e])this.callStreamMain=null;this.callStreamUsers[e]=null;clearTimeout(this.initPeerConnectionTimeout[e]);return true};t.webrtc.prototype.deleteEvents=function(){this.initiator=false;this.callUserId=0;this.callChatId=0;this.callToGroup=false;this.callGroupUsers=[];this.callInit=false;this.callInitUserId=0;this.callActive=false;this.callVideo=false;this.callRequestUserMedia={};this.audioMuted=false;this.videoMuted=false;this.needPeerConnection=true;var e=0;for(e in this.pc){if(this.pc.hasOwnProperty(e)){this.pc[e].close();delete this.pc[e]}}this.pc={};this.pcStart={};this.connected={};this.iceCandidates={};for(e in this.iceCandidateTimeout){if(this.iceCandidateTimeout.hasOwnProperty(e)){clearTimeout(this.iceCandidateTimeout[e])}}this.iceCandidateTimeout={};for(e in this.pcConnectTimeout){if(this.pcConnectTimeout.hasOwnProperty(e)){clearTimeout(this.pcConnectTimeout[e])}}this.pcConnectTimeout={};for(e in this.createAnswerTimeout){if(this.createAnswerTimeout.hasOwnProperty(e)){clearTimeout(this.createAnswerTimeout[e])}}this.createAnswerTimeout={};for(e in this.initPeerConnectionTimeout){if(this.initPeerConnectionTimeout.hasOwnProperty(e)){clearTimeout(this.initPeerConnectionTimeout[e])}}this.initPeerConnectionTimeout={};if(this.callStreamSelf){t.webrtc.stopMediaStream(this.callStreamSelf);this.callStreamSelf=null}if(this.callStreamMain){t.webrtc.stopMediaStream(this.callStreamMain);this.callStreamMain=null}for(e in this.callStreamUsers){if(this.callStreamUsers.hasOwnProperty(e)){if(this.callStreamUsers[e])t.webrtc.stopMediaStream(this.callStreamUsers[e]);delete this.callStreamUsers[e]}}};t.webrtc.prototype.mergeConstraints=function(e,t){if(!this.ready())return false;var i=e;for(var n in t.mandatory){if(t.mandatory.hasOwnProperty(n)){i.mandatory[n]=t.mandatory[n]}}i.optional.concat(t.optional);return i};t.webrtc.prototype.sendOfferToPeer=function(e){if(!this.pc[e])return false;var i={optional:[],mandatory:{MozDontOfferDataChannel:true}};if(this.detectedBrowser==="chrome"){for(var n in i.mandatory){if(i.mandatory.hasOwnProperty(n)){if(n.indexOf("Moz")!=-1)delete i.mandatory[n]}}}this.log("Constraints",i);i=this.mergeConstraints(i,this.sdpConstraints);this.log("Sending offer to peer, with constraints:",e,'"'+JSON.stringify(i)+'".');this.pc[e].createOffer(t.delegate(function(t){this.setLocalAndSend(e,t)},this),t.delegate(function(e){this.log("createOffer failure",e)},this),i);return true};t.webrtc.prototype.createPeerConnection=function(e){try{this.pc[e]=new RTCPeerConnection(this.pcConfig,this.pcConstraints);this.pc[e].onicecandidate=t.delegate(function(t){this.onIceCandidateEvent(e,t)},this);this.pc[e].onaddstream=t.delegate(function(t){this.onRemoteStreamAddedEvent(e,t)},this);this.pc[e].onremovestream=t.delegate(function(t){this.onRemoteStreamRemovedEvent(e,t)},this);this.pc[e].oniceconnectionstatechange=t.delegate(function(t){this.onIceConnectionStateChangeEvent(e,t)},this);this.pc[e].onsignalingstatechange=t.delegate(function(t){this.onSignalingStateChangeEvent(e,t)},this);if(!this.oneway||this.initiator){this.pc[e].addStream(this.callStreamSelf)}this.log("Created RTCPeerConnnection for "+e+" with:\n"+'  config: "'+JSON.stringify(this.pcConfig)+'";\n'+'  constraints: "'+JSON.stringify(this.pcConstraints)+'".')}catch(t){if(this.callToGroup&&this.callStreamUsers[e])return false;this.log("PeerConnection: ",JSON.stringify(this.pcConfig),JSON.stringify(this.pcConstraints));this.log("Failed to create PeerConnection, exception: "+t.message);this.peerConnectionError(e,t)}return true};t.webrtc.prototype.signalingPeerData=function(e,t){var i=JSON.parse(t);if(i.type==="offer"){if(!this.pcStart[e])this.initPeerConnection(e);this.createAnswer(e,i)}else if(i.type==="answer"&&this.pcStart[e]){if(this.pc[e]==null)return false;this.pc[e].setRemoteDescription(new RTCSessionDescription(i),function(){},function(){})}else if(i.type==="candidate"&&this.pcStart[e]){if(!this.pc[e]||this.pc[e]==null)return false;for(var n=0;n<i.candidates.length;n++){var r=new RTCIceCandidate({sdpMLineIndex:i.candidates[n].label,candidate:i.candidates[n].candidate});try{this.pc[e].addIceCandidate(r)}catch(t){this.log("Error addIceCandidate",JSON.stringify(t));this.peerConnectionReconnect(e);return false}}}else{this.log('Signaling command "'+(i&&i.type?i.type:"undefined")+'" from user "'+e+'" skip')}return true};t.webrtc.prototype.onRemoteStreamAddedEvent=function(e,t){if(!this.pc[e])return false;this.log("Remote stream added",e,JSON.stringify(t));var i=false;if(!this.callStreamMain){this.callStreamMain=t.stream;i=true}this.callStreamUsers[e]=t.stream;this.onRemoteStreamAdded(e,t,i)};t.webrtc.prototype.onRemoteStreamRemovedEvent=function(e,t){if(!this.pc[e])return false;this.callStreamUsers[e]=null;this.onRemoteStreamRemoved(e,t)};t.webrtc.prototype.onIceCandidateEvent=function(e,i){if(!this.pc[e])return false;if(!this.iceCandidates[e])this.iceCandidates[e]=[];if(i.candidate){this.log("New ICE candidate: ",i.candidate);this.iceCandidates[e].push({type:"candidate",label:i.candidate.sdpMLineIndex,id:i.candidate.sdpMid,candidate:i.candidate.candidate});clearTimeout(this.iceCandidateTimeout[e]);this.iceCandidateTimeout[e]=setTimeout(t.delegate(function(){if(this.iceCandidates[e].length===0)return false;this.onIceCandidate(e,{type:"candidate",candidates:this.iceCandidates[e]});this.iceCandidates[e]=[]},this),250)}else{this.log("End of candidates of "+e)}};t.webrtc.prototype.onIceConnectionStateChangeEvent=function(e,i){clearTimeout(this.pcConnectTimeout[e]);if(!this.pc[e]||this.pc[e].iceConnectionState=="close")return false;this.log("iceConnectionStateChange",e,this.pc[e].iceConnectionState,this.pc[e].signalingState);this.pcConnectTimeout[e]=setTimeout(t.delegate(function(){this.peerConnectionReconnect(e)},this),15e3);this.onIceConnectionStateChange(e,i);return true};t.webrtc.prototype.onSignalingStateChangeEvent=function(e,i){clearTimeout(this.pcConnectTimeout[e]);if(!this.pc[e]||this.pc[e].signalingState=="close")return false;this.log("signalingStateChange",e,this.pc[e].iceConnectionState,this.pc[e].signalingState);this.pcConnectTimeout[e]=setTimeout(t.delegate(function(){this.peerConnectionReconnect(e)},this),15e3);this.onSignalingStateChange(e,i)};t.webrtc.prototype.createAnswer=function(e,i){if(!this.callActive)return false;if(!this.pcStart[e]){clearTimeout(this.createAnswerTimeout[e]);this.createAnswerTimeout[e]=setTimeout(t.delegate(function(){this.createAnswer(e,i)},this),2e3);return false}this.pc[e].setRemoteDescription(new RTCSessionDescription(i),t.delegate(function(){if(!this.pc[e])return false;this.pc[e].createAnswer(t.delegate(function(t){this.setLocalAndSend(e,t)},this),t.delegate(function(e){this.log("createAnswer failure",e)},this),this.sdpConstraints)},this),function(){});return true};t.webrtc.prototype.setDefaultCamera=function(e){this.defaultCamera=e};t.webrtc.prototype.setDefaultMicrophone=function(e){this.defaultMicrophone=e};t.webrtc.prototype.logDevices=function(){var e=this;if(navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices){e.log("Enumerating media devices");navigator.mediaDevices.enumerateDevices().then(function(t){t.forEach(function(t){try{e.log(JSON.stringify(t))}catch(i){e.log(t)}})})}else{e.log("Could not enumerate devices, api is not supported")}};t.webrtc.mediaStreamTrackToString=function(e){var i="";for(key in e){if(!t.type.isFunction(e[key])){i=i+" "+key+": "+e[key]+";"}}return i};t.webrtc.stopMediaStream=function(e){if(!(e instanceof MediaStream))return;if(typeof e.getTracks==="undefined"){e.stop()}else{e.getTracks().forEach(function(e){e.stop()})}}})(window);
//# sourceMappingURL=core_webrtc.map.js